package com.juzipi.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.juzipi.demo.pojo.People;
import com.juzipi.demo.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * @Author juzipi
 * @Date 2021/4/25 13:21
 * @Info
 */
@RestController
@RequestMapping("people")
public class PeopleController {


    @Autowired
    private PeopleService peopleService;



    @GetMapping("list")
    public List<People> getPeopleList(){
        return peopleService.queryAllPeople();
    }

    @GetMapping("get/{id}")
    public People getPeople(@PathVariable Long id){
        return peopleService.queryPeopleById(id);
    }


    @PostMapping("add")
    public Integer insertPeople(@RequestBody People people){
        return peopleService.createPeople(people);
    }

    @PutMapping("set")
    public Integer updatePeople(@RequestBody People people){
        return peopleService.modifyPeople(people);
    }


    @DeleteMapping("del/{id}")
    public Integer deletePeople(@PathVariable Long id){
        return peopleService.removePeopleById(id);
    }


    @GetMapping("getList")
    public List<People> getList(){
        return peopleService.list();
    }

    @PostMapping("getCount")
    public Integer getCount(@RequestParam String name){
        return peopleService.count(new QueryWrapper<People>().lambda().eq(People::getName, name));
    }


    @PostMapping("getOne")
    public People getOne(@RequestParam String name){
        //还能这样玩，获取到mapper
        //不用它封装的service方法，自己写,所有操作都放到了controller，着实用点臃肿了
        BaseMapper<People> baseMapper = peopleService.getBaseMapper();
        return baseMapper.selectOne(new QueryWrapper<People>().lambda().eq(People::getName, name));
    }

    @PostMapping("insertOne")
    public Boolean insertOne(@RequestBody People people){
        return peopleService.save(people);
    }

    @PostMapping("updateOne")
    public Boolean updateOne(@RequestBody People people){
        return peopleService.updateById(people);
    }

    @PostMapping("GetMap")
    public List<Map<String, Object>> get(@RequestParam String name){
        List<Map<String, Object>> maps = peopleService.listMaps(new QueryWrapper<People>().lambda().eq(People::getName, name));

        System.out.println("_____________________"+maps);
        //返回值看不出来有什么。遍历一下看看
        maps.forEach(stringObjectMap ->
            stringObjectMap.forEach((key,value)-> System.out.println("key:"+key+","+"value:"+value))
        );
        return maps;
    }


}
